<?php
require_once APP . '/application/models/Alllist.php';

/**
 * 前台专用控制器
、 * 设置默认的视图目录,设置默认视图扩展名为php
 * @package front
 */
class FrontAction extends Request
{
    
	/**
	 * 系统配置信息
	 * 
	 * @var Array
	 */
    public $cfg;
    
    /**
     * 数据库对象
     *
     * @var Mysql
     */
    public $db;
    
    /**
     * 重载控制器
     * 定义默认的视图路径，文件编码，视图扩展名
     * 并调用权限认证
     */
    public function __construct (Route $route) {
        parent::__construct($route);
        $this->setViewPath(dirname ($route->getControllerPath ()) . '/views')//设置视图路径
             ->setAutoRender (false)//设置是否自动渲染页面
             ->setSuffix ('.php');//设置视图后缀名
        
        $controllerName         = $this->getParam('controller');
        $actionName             = $this->getParam('action');
        
        $this->view->baseUrl    = $this->getBaseUrl ();
        $this->view->fullUrl    = $this->getHost() . $this->getBaseUrl ();
        $this->view->controllerName = $controllerName;
        
        global $cfg;
        $this->cfg = $cfg;
        $this->view->cfg = $cfg;
        $this->db = Mysql::getInstance();
        
        $this->header();
    }
    
    
    /**
     * 输出错误信息
     */
    public function showmessage ($message='', $type='') {
        $message = array("status" => 0, "message" => $message);
        if($type != "")
        	$message['type'] = $type;
    	
    	echo json_encode($message);
    	exit;
    }
    
    /**
     * 内部跳转
     *
     * @param unknown_type $controller
     * @param unknown_type $action
     * @param unknown_type $param
     */
    protected function redirectAction($controller, $action, $param = '') {
    	$url = $this->url($this->view->baseUrl, $controller, $action, $param);
    	header("location:" . $url);
    	exit;
    }
    
    /**
     * 处理头部数据
     *
     */
    private function header()
    {
    	$obj_list = new Alllist($this->db);
    	
    	$types = array();
    	// 中心概况
    	$types['zxgk'] = $obj_list->getAllListByType($this->cfg['zxgk_list_type'], false);
    	// 组织架构
    	$types['zzjg'] = $obj_list->getAllListByType($this->cfg['zzjg_list_type'], false);
    	// 访问学者
    	$types['fwxz'] = $obj_list->getAllListByType($this->cfg['fwxz_list_type'], false);
    	// 出版讯息
    	$types['publication'] = $obj_list->getAllListByType($this->cfg['publication_list_type'], false);

    	// 读取图片新闻
    	$sql = 'select aid,title,image,subtitle from article where cid=' . $this->cfg['article_types']['tpxw']['id'] . ' and image !="" order by seq desc,aid desc limit 5';
    	$arr_article_image = $this->db->fetchAll($sql);
    	foreach ($arr_article_image as $key => $v){
    		$arr_article_image[$key]['url'] = $v['subtitle'];
    		$arr_article_image[$key]['image'] = $this->view->baseUrl . $arr_article_image[$key]['image'];
    	}
    	
    	// 读取友情链接
    	require_once(APP . '/application/models/LinkModel.php');
		$obj_link = new LinkModel($this->db);
		$result = $obj_link->fetchAll(1, 100, 1);
		$this->view->links = $result['list'];
		
    	$this->view->types = $types;
    	$this->view->headerimages = $arr_article_image;
    }
}